package com.yyx.server.controller;

import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.yyx.server.utils.MD5Util;
import com.yyx.server.utils.RandImageUtil;
import com.yyx.server.vo.ResultVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;

/**
 * @author YongXin
 * @date Created in 2021/7/27 17:26
 */
@RestController
public class CaptchaController {
    @Autowired
    private DefaultKaptcha defaultKaptcha;
    @ApiOperation("验证码 -- 流形式")
    @GetMapping(value = "/captcha",produces ="image/jpeg" )
    public void captcha(HttpServletRequest request, HttpServletResponse response){
        //-----------------处理响应头----------------
        // 定义response输出类型为image/jpeg类型
        response.setDateHeader("Expires", 0);
        // Set standard HTTP/1.1 no-cache headers.
        response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
        // Set IE extended HTTP/1.1 no-cache headers (use addHeader).
        response.addHeader("Cache-Control", "post-check=0, pre-check=0");
        // Set standard HTTP/1.0 no-cache header.
        response.setHeader("Pragma", "no-cache");
        // return a jpeg
        response.setContentType("image/jpeg");
        //-----------------生成验证码----------------
        //生成验证码文本
        String text = defaultKaptcha.createText();
        System.out.println("验证码 = " + text);
        //验证码文本存入session
        HttpSession session = request.getSession();
        session.setAttribute("captcha",text);
        //根据验证码文本创建图形验证码
        BufferedImage kaptchaImage = defaultKaptcha.createImage(text);
        ServletOutputStream outputStream = null;
        try {
            outputStream = response.getOutputStream();
            ImageIO.write(kaptchaImage,"jpg", outputStream);
            outputStream.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    @ApiOperation("验证码 -- base64")
    @GetMapping(value = "/randomImage")
    public ResultVo getCaptcha(HttpServletRequest request){
        ResultVo resultVo = new ResultVo();
        try {
            //生成验证码文本
            String code = defaultKaptcha.createText();
            System.out.println("验证码 = " + code);
            //验证码文本存入session
            HttpSession session = request.getSession();
            session.setAttribute("captcha",code);
            String lowerCaseCode = code.toLowerCase();
            String realKey = MD5Util.MD5Encode(lowerCaseCode, "utf-8");
            String base64 = RandImageUtil.generate(code);
            resultVo.setData(base64);
            resultVo.setCode(200);
        } catch (Exception e) {
            resultVo.setMessage("获取验证码出错");
            resultVo.setCode(500);
            e.printStackTrace();
        }
        return resultVo;
    }
}
